package com.huatai.datadevelop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huatai.common.api.R;
import com.huatai.common.error.exception.ServiceException;
import com.huatai.datadevelop.entity.ExecuteEntity;
import com.huatai.datadevelop.service.ExecuteService;
import com.huatai.datadevelop.vo.DeleteVO;
import com.huatai.datadevelop.vo.InterfaceNameComplianceVO;
import com.huatai.datadevelop.vo.TableNameToSQLVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hecai
 * @description: TODO  执行sql语句
 * @date 2022/12/27 10:58
 * @Version 1.0
 */
@Api(value = "执行SQL模块",tags = "执行SQL模块")
@RestController
@RequestMapping("/execute")
public class ExecuteSqlController {
    @Autowired
    private ExecuteService executeService;

    /**
     * 获取英文名称是否合规
     * @param interfaceNameComplianceVO
     * @return
     */
	@ApiOperation("获取英文名称是否合规")
    @PostMapping("getInterfaceNameCompliance")
    public R getInterfaceNameCompliance(@RequestBody InterfaceNameComplianceVO interfaceNameComplianceVO){
        return R.data(this.executeService.getTableNameCompliance(interfaceNameComplianceVO.getInterfaceName()));
    }

    /**
     * 保存或更新
     * @param executeEntity
     * @return
     */
	@ApiOperation("数据新增或修改")
    @PostMapping("saveOrUpdate")
    public R saveOrUpdate(@RequestBody ExecuteEntity executeEntity){
        return R.data( this.executeService.saveOrUpdate(executeEntity));
    }

    /**
     * 根据主键id删除数据
     * @param
     * @return
     */
	@ApiOperation("根据主键id删除数据")
    @PostMapping("deleteByIds")
    public R deleteByIds(@RequestBody DeleteVO vo){
        return R.data( this.executeService.removeByIds(vo.getIds()));
    }

    /**
     * 查询数据
     * @param params
     * @return
     */
	@ApiOperation("查询数据")
    @PostMapping("list")
    public R page(@RequestBody Map<String, Object> params){
        return R.data( this.executeService.queryPage(params));
    }

    /**
     * 根据表名称自动装配sql
     * @param vo
     * @return
     */
	@ApiOperation("根据表名称自动装配sql")
    @PostMapping("getTableNameToSQL")
    public R getTableNameToSQL(@RequestBody TableNameToSQLVO vo){
        HashMap<String, String> tableNameToSQL = this.executeService.getTableNameToSQL(vo.getTableName());
        return R.data(tableNameToSQL);
    }

    /**
     * 获取数据
     * @param params
     * @param interfaceName
     * @return
     */
	@ApiOperation("获取数据")
    @PostMapping("asyncRequestApi/{interfaceName}")
    public R asyncRequestApi(@RequestBody Map<String, Object> params,
                             @PathVariable("interfaceName") String interfaceName){
        QueryWrapper<ExecuteEntity> qw = new QueryWrapper<>();
        qw.eq("interface_name",interfaceName);
        ExecuteEntity entity = this.executeService.getOne(qw);
        if(entity==null){
            throw new ServiceException("查无>>>>"+interfaceName+"<<<<此接口，请核查");
        }
        return R.data(this.executeService.asyncRequestApi(params,entity,new HashMap<String,Object>()));
    }
}
